package io.renren.modules.substation.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.substation.entity.*;
import io.renren.modules.substation.service.TBaseDictInfoService;
import io.renren.modules.substation.service.TInfoAlarmConfigService;
import io.renren.modules.substation.service.TInfoDevInfoService;
import io.renren.modules.substation.service.TInfoVariableInfoService;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;



/**
 * 告警阈值配置表
 *
 * @author yzx
 * @email sunlightcs@gmail.com
 * @date 2020-06-19 16:55:28
 */
@RestController
@RequestMapping("substation/tinfoalarmconfig")
public class TInfoAlarmConfigController extends AbstractController {
    @Autowired
    private TInfoAlarmConfigService tInfoAlarmConfigService;

    @Autowired
    private TInfoDevInfoService tInfoDevInfoService;

    @Autowired
    private TBaseDictInfoService tBaseDictInfoService;

    @Autowired
    private TInfoVariableInfoService tInfoVariableInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("substation:tinfoalarmconfig:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = tInfoAlarmConfigService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("substation:tinfoalarmconfig:info")
    public R info(@PathVariable("id") Long id){
		TInfoAlarmConfigEntity tInfoAlarmConfig = tInfoAlarmConfigService.getById(id);

        return R.ok().put("tInfoAlarmConfig", tInfoAlarmConfig);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("substation:tinfoalarmconfig:save")
    public R save(@RequestBody TInfoAlarmConfigEntity tInfoAlarmConfig){

        tInfoAlarmConfig.setCreateOpr(getUserId());
        tInfoAlarmConfig.setLastOpr(getUserId());
        tInfoAlarmConfig.setLastTime(new Date());
        tInfoAlarmConfig.setCreateTime(new Date());


        tInfoAlarmConfigService.save(tInfoAlarmConfig);
        tInfoAlarmConfigService.refreshCacheAlarmConfig();//更新告警配置缓存

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("substation:tinfoalarmconfig:update")
    public R update(@RequestBody TInfoAlarmConfigEntity tInfoAlarmConfig){
		tInfoAlarmConfigService.updateById(tInfoAlarmConfig);
        tInfoAlarmConfigService.refreshCacheAlarmConfig();//更新告警配置缓存

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("substation:tinfoalarmconfig:delete")
    public R delete(@RequestBody Long[] ids){
		tInfoAlarmConfigService.removeByIds(Arrays.asList(ids));
        tInfoAlarmConfigService.refreshCacheAlarmConfig();//更新告警配置缓存

        return R.ok();
    }

    
    @RequestMapping("/getVarListByDevCode")
    public R getVarListByDevCode(@RequestParam String devCode) {

        logger.debug("variableCodeNamelist start "+ devCode);
        Map<String, Object> result = new HashMap<>();

        TInfoDevInfoEntity query_dev=new TInfoDevInfoEntity();
        query_dev.setDevCode(devCode);
        TInfoDevInfoEntity devInfo=tInfoDevInfoService.queryDevInfoByCondition(query_dev);
        TInfoVariableInfoEntity var_query=new  TInfoVariableInfoEntity();
        var_query.setVariableType(devInfo.getDevType());
        if(devInfo.getDevType().equals("01"))
        {
            //查询遥信数据
            var_query.setVariableTypeFl("0");
        }
        //var_query.setVariableKind("00");

        SelectItemInfo temp0=new SelectItemInfo("--请选择--","");
        List<TInfoVariableInfoEntity> variablelist = tInfoVariableInfoService.queryVariableByCondition(var_query);
        List<SelectItemInfo> variableCodeNamelist = new ArrayList<>();
        variableCodeNamelist.add(temp0);

        //获取局放信息
        for(TInfoVariableInfoEntity o : variablelist){
            if(o.getVariableName().equals(ConstChinese.CH_JF))
            {
                List<TBaseDictInfoEntity> jf_child_List =tBaseDictInfoService.queryByDictKey("JF_CHILD");
                for(TBaseDictInfoEntity d : jf_child_List){
                    String name=o.getVariableName()+d.getDictDesc();
                    String value="J"+o.getVariableId().toString()+"-"+d.getDictValue();
                    SelectItemInfo m = new SelectItemInfo(name,value);
                    variableCodeNamelist.add(m);
                }

            }

        }
        for(TInfoVariableInfoEntity o : variablelist){

            if(o.getVariableName().equals(ConstChinese.CH_JF))
            {

            }
            else
            {
                //普通开关量
                if(o.getVariableKind().equals("00"))
                {
                    SelectItemInfo m = new SelectItemInfo(o.getVariableName(),o.getVariableId().toString());
                    variableCodeNamelist.add(m);
                }
                else
                {
                    //环境量
                    String name="环境量"+Integer.parseInt(o.getVariableKind())+"-"+o.getVariableName();
                    String value="E"+o.getVariableKind()+"-"+o.getVariableId().toString();
                    SelectItemInfo m = new SelectItemInfo(name,value);
                    variableCodeNamelist.add(m);
                }
            }
        }


        result.put("variableCodeNamelist", variableCodeNamelist);

        return R.ok().put("result", result);
    }


    @RequestMapping("/getVarListByDevCodeForAlam")
    public R getVarListByDevCodeForAlam(@RequestParam String devCode) {

        logger.debug("variableCodeNamelist start "+ devCode);
        Map<String, Object> result = new HashMap<>();

        TInfoDevInfoEntity query_dev=new TInfoDevInfoEntity();
        query_dev.setDevCode(devCode);
        TInfoDevInfoEntity devInfo=tInfoDevInfoService.queryDevInfoByCondition(query_dev);
        TInfoVariableInfoEntity var_query=new  TInfoVariableInfoEntity();
        var_query.setVariableType(devInfo.getDevType());
        if(devInfo.getDevType().equals("01"))
        {
            //查询遥测试数据
            var_query.setVariableTypeFl("1");
        }
        //var_query.setVariableKind("00");

        SelectItemInfo temp0=new SelectItemInfo("--请选择--","");
        List<TInfoVariableInfoEntity> variablelist = tInfoVariableInfoService.queryVariableByCondition(var_query);
        List<SelectItemInfo> variableCodeNamelist = new ArrayList<>();
        variableCodeNamelist.add(temp0);

        //获取局放信息
        for(TInfoVariableInfoEntity o : variablelist){
            if(o.getVariableName().equals(ConstChinese.CH_JF))
            {
                List<TBaseDictInfoEntity> jf_child_List =tBaseDictInfoService.queryByDictKey("JF_CHILD");
                for(TBaseDictInfoEntity d : jf_child_List){
                    String name=o.getVariableName()+d.getDictDesc();
                    String value="J"+o.getVariableId().toString()+"-"+d.getDictValue();
                    SelectItemInfo m = new SelectItemInfo(name,value);
                    variableCodeNamelist.add(m);
                }

            }

        }
        for(TInfoVariableInfoEntity o : variablelist){

            if(o.getVariableName().equals(ConstChinese.CH_JF))
            {

            }
            else
            {
                //普通开关量
                if(o.getVariableKind().equals("00"))
                {
                    SelectItemInfo m = new SelectItemInfo(o.getVariableName(),o.getVariableId().toString());
                    variableCodeNamelist.add(m);
                }
                else
                {
                    //环境量
                    String name="环境量"+Integer.parseInt(o.getVariableKind())+"-"+o.getVariableName();
                    String value="E"+o.getVariableKind()+"-"+o.getVariableId().toString();
                    SelectItemInfo m = new SelectItemInfo(name,value);
                    variableCodeNamelist.add(m);
                }
            }
        }


        result.put("variableCodeNamelist", variableCodeNamelist);

        return R.ok().put("result", result);
    }

    @GetMapping("/getInitList")
    public R getInitList(){
        logger.debug("getInitList start ");
        SelectItemInfo temp0=new SelectItemInfo("--请选择--","");

        logger.debug("devCodeNamelist start ");
        List<TInfoDevInfoEntity> devInfolist = tInfoDevInfoService.list();
        List<SelectItemInfo> devCodeNamelist = new ArrayList<>();
        devCodeNamelist.add(temp0);
        for(TInfoDevInfoEntity o : devInfolist){
            SelectItemInfo m = new SelectItemInfo(o.getDevName(),o.getDevCode());
            //logger.debug(o.getDevName());
            devCodeNamelist.add(m);
        }

        logger.debug("devIdCodelist start ");
        List<SelectItemInfo> devIdCodelist = new ArrayList<>();
        devIdCodelist.add(temp0);
        for(TInfoDevInfoEntity o : devInfolist){
            SelectItemInfo m = new SelectItemInfo(o.getDevCode(),o.getId().toString());
            //logger.debug(o.getDevName());
            devIdCodelist.add(m);
        }

        logger.debug("devTypeCodelist start ");
        List<SelectItemInfo> devTypeCodelist = new ArrayList<>();
        devTypeCodelist.add(temp0);
        for(TInfoDevInfoEntity o : devInfolist){
            SelectItemInfo m = new SelectItemInfo(o.getDevCode(),o.getDevType());
            //logger.debug(o.getDevName());
            devTypeCodelist.add(m);
        }



        logger.debug("variableCodeNamelist start ");
        List<TInfoVariableInfoEntity> variablelist = tInfoVariableInfoService.list();
        List<SelectItemInfo> variableCodeNamelist = new ArrayList<>();
        variableCodeNamelist.add(temp0);
        for(TInfoVariableInfoEntity o : variablelist){
            SelectItemInfo m = new SelectItemInfo(o.getVariableName(),o.getVariableId().toString());
            //logger.debug(o.getVariableName());
            variableCodeNamelist.add(m);
        }



        logger.debug("devPeCodeNamelist start ");
        TInfoDevInfoEntity tInfoDevInfoEntity = new TInfoDevInfoEntity();
        tInfoDevInfoEntity.setDevType("01");
        List<TInfoDevInfoEntity> devPelist = tInfoDevInfoService.queryDevListByCondition(tInfoDevInfoEntity);
        List<SelectItemInfo> devPeCodeNamelist = new ArrayList<>();
        devPeCodeNamelist.add(temp0);
        for(TInfoDevInfoEntity o : devPelist){
            SelectItemInfo m = new SelectItemInfo(o.getDevName(),o.getDevCode());
            //logger.debug(o.getDevName());
            devPeCodeNamelist.add(m);
        }


        logger.debug("getInitList end ");

        Map<String, Object> result = new HashMap<>();
        result.put("devCodeNamelist", devCodeNamelist);
        result.put("devIdCodelist", devIdCodelist);
        result.put("variableCodeNamelist", variableCodeNamelist);
        result.put("devPeCodeNamelist", devPeCodeNamelist);

        return R.ok().put("result", result);
    }


    @RequestMapping("/getVarListByDevCodeAll")
    public R getVarListByDevCodeAll(@RequestParam String devCode) {

        logger.debug("variableCodeNamelistAll start "+ devCode);
        Map<String, Object> result = new HashMap<>();

        TInfoDevInfoEntity query_dev=new TInfoDevInfoEntity();
        query_dev.setDevCode(devCode);
        TInfoDevInfoEntity devInfo=tInfoDevInfoService.queryDevInfoByCondition(query_dev);
        TInfoVariableInfoEntity var_query=new  TInfoVariableInfoEntity();
        var_query.setVariableType(devInfo.getDevType());
//        if(devInfo.getDevType().equals("01"))
//        {
//            //查询遥信数据
//            var_query.setVariableTypeFl("0");
//        }
        //var_query.setVariableKind("00");

        SelectItemInfo temp0=new SelectItemInfo("--请选择--","");
        List<TInfoVariableInfoEntity> variablelist = tInfoVariableInfoService.queryVariableByCondition(var_query);
        List<SelectItemInfo> variableCodeNamelist = new ArrayList<>();
        variableCodeNamelist.add(temp0);

        //获取局放信息
        for(TInfoVariableInfoEntity o : variablelist){
            if(o.getVariableName().equals(ConstChinese.CH_JF))
            {
                List<TBaseDictInfoEntity> jf_child_List =tBaseDictInfoService.queryByDictKey("JF_CHILD");
                for(TBaseDictInfoEntity d : jf_child_List){
                    String name=o.getVariableName()+d.getDictDesc();
                    String value="J"+o.getVariableId().toString()+"-"+d.getDictValue();
                    SelectItemInfo m = new SelectItemInfo(name,value);
                    variableCodeNamelist.add(m);
                }

            }

        }
        for(TInfoVariableInfoEntity o : variablelist){

            if(o.getVariableName().equals(ConstChinese.CH_JF))
            {

            }
            else
            {
                //普通开关量
                if(o.getVariableKind().equals("00"))
                {
                    SelectItemInfo m = new SelectItemInfo(o.getVariableName(),o.getVariableId().toString());
                    variableCodeNamelist.add(m);
                }
                else
                {
                    //环境量
                    String name="环境量"+Integer.parseInt(o.getVariableKind())+"-"+o.getVariableName();
                    String value="E"+o.getVariableKind()+"-"+o.getVariableId().toString();
                    SelectItemInfo m = new SelectItemInfo(name,value);
                    variableCodeNamelist.add(m);
                }
            }
        }


        result.put("variableCodeNamelist", variableCodeNamelist);

        return R.ok().put("result", result);
    }


}
